package com.example.demo.web.controller;

import com.example.demo.pojo.Employee;
import com.example.demo.service.IEmployeeService;
import com.example.demo.vo.PageForm;
import com.example.demo.vo.ResultData;
import com.example.demo.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("employee")
public class EmployeeController {

    @Autowired
    BCryptPasswordEncoder bCryptPasswordEncoder;


    @Autowired
    IEmployeeService employeeService;

    @GetMapping("sale/list")
    public ResultData selectSalePersonPage(PageForm<Employee> page,String name,String phone){
        ResultVO resultVO = employeeService.selectSalePersonPage(page.createPage(),name,phone);
        return resultVO;
    }

    @GetMapping("del")
    public ResultData delEmployee(Integer employeeId){
        boolean flag = employeeService.delEmployeeById(employeeId);
        return ResultData.success(flag);
    }


    @GetMapping("/add")
    public ResultData addEmployee(Employee employee){
        employee.setPassword(bCryptPasswordEncoder.encode(employee.getPassword()));
        boolean flag = employeeService.addEmployee(employee);
        return ResultData.success(flag);
    }

    @GetMapping("dels")
    public ResultData delEmployees(Integer[] ids){
        boolean flag = employeeService.delEmployeeByIds(ids);
        return ResultData.success(flag);
    }

}
